<template>
  <div class="info">
    <router-link class="title" :to="{name:'video', params:{id:video.id}}">
      {{ video.title }}
    </router-link>
    <router-link class="cover" :to="{name:'video', params:{id:video.id}}">
      <img :src="video.posterSrc" :alt="video.title">
    </router-link>
    <button v-if="isIndex" @click="likeVideo(video.id)">
      <font-awesome-icon :icon="faHeart" class="fa-heart" :class="{'fond':isFond}"/>
    </button>
  </div>
</template>

<script setup lang="ts">
import {Video} from '@/types'
import {computed, onMounted, ref} from 'vue'
import {useRoute} from 'vue-router'
import {useVideoStore} from '@/stores'
import {library} from '@fortawesome/fontawesome-svg-core'
import {faHeart} from '@fortawesome/free-solid-svg-icons/faHeart'

library.add(faHeart)

const route = useRoute()
const store = useVideoStore()
const props = defineProps<{ video: Video }>()

let isIndex = computed<boolean>(() => route.path === '/' || route.path === '/home')
let isFond = ref<boolean>(false)
onMounted(() => {
  isFond.value = props.video.isFond || false
})

const likeVideo = (id: number) => {
  let fondList = JSON.parse(<string>localStorage.getItem('fondList')) || []
  const index = fondList.indexOf(id)
  if (index === -1) {
    fondList.unshift(id)
    isFond.value = true
  } else {
    fondList.splice(index, 1)
    isFond.value = false
  }
  localStorage.setItem('fondList', JSON.stringify(fondList))
}
</script>

<style lang="less" scoped>
.info {
  position: relative;
  float: left;
  width: 30%;
  background-color: rgba(255, 255, 255, .4);
  margin: 0 16px;
  margin-bottom: 25px;
  box-sizing: border-box;
  border: solid rgba(0, 0, 0, 0.1) 1px;
  border-radius: 5px;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.4);
  transition: all 0.4s;

  .title {
    color: #4042a1cc;
    font-size: 16px;
    line-height: 30px;
    margin-left: 10px;
  }

  .cover {
    display: block;
    width: 98%;
    margin: 0 auto;
    padding-bottom: 5px;

    img {
      width: 100%;
      aspect-ratio: 1.77777;
      border-radius: 5px;
      box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
    }
  }

  &:hover {
    background-color: rgba(0, 0, 0, .2);
    box-shadow: 0 5px 15px rgb(0 0 0 / 50%)
  }

  &:hover .title {
    color: #be6060;
  }
}

button {
  width: 25px;
  height: 25px;
  border: none;
  background-color: rgba(255, 255, 255, .4);
  border-radius: 50%;
  cursor: pointer;

  position: absolute;
  right: 5px;
  bottom: 12px;

  .fa-heart {
    color: gray;
    position: absolute;
    left: 6px;
    top: 7px;
  }

  .fond {
    color: #EA5757D2;
  }
}
</style>